package com.railway.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * JSON工具类
 * @author wuyb
 *
 */
public class Json
{

    public Json()
    {
    }

    public static String toJson(Object obj)
    {
        return toJson(getAttributes(obj));
    }

    public static String toJson(Map map)
 {
		StringBuilder sb = new StringBuilder();
		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
			String s1 = (String) iterator.next();
			Object obj = map.get(s1);
			if (obj == null) {
				sb.append("\"").append(s1).append("\":null,");
			} else if (obj instanceof Boolean) {
				Boolean boolean1 = (Boolean) obj;
				sb.append("\"").append(s1).append("\":").append(
						boolean1.toString()).append(",");
			} else if ((obj instanceof Integer) || (obj instanceof Long)
					|| (obj instanceof Float) || (obj instanceof Double)
					|| (obj instanceof Short) || (obj instanceof BigInteger)
					|| (obj instanceof BigDecimal)) {
				sb.append("\"").append(s1).append("\":").append(obj.toString())
						.append(",");
			} else if (obj instanceof String) {
				String s2 = (String) obj;
				s2 = s2.replaceAll("\\\\", "\\\\\\\\");
				s2 = s2.replaceAll("\n", "\\\\n");
				s2 = s2.replaceAll("\r", "\\\\r");
				s2 = s2.replaceAll("\"", "\\\\\"");
				s2 = s2.replaceAll("'", "\\\\'");
				sb.append("\"").append(s1).append("\":").append("\"")
						.append(s2).append("\",");
			} else if (obj instanceof Date) {
				SimpleDateFormat simpledateformat = new SimpleDateFormat(
						"yyyy-MM-dd");
				Date date = (Date) obj;
				String s4 = simpledateformat.format(new java.util.Date(date
						.getTime()));
				sb.append("\"").append(s1).append("\":").append("\"")
						.append(s4).append("\",");
			} else if (obj instanceof Timestamp) {
				SimpleDateFormat simpledateformat1 = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Timestamp timestamp = (Timestamp) obj;
				String s5 = simpledateformat1.format(new java.util.Date(
						timestamp.getTime()));
				sb.append("\"").append(s1).append("\":").append("\"")
						.append(s5).append("\",");
			} else if (obj instanceof List) {
				String s3 = toJson((List) obj);
				sb.append("\"").append(s1).append("\":").append(s3).append(",");
			} else if (obj instanceof Object[]) {
				String s3 = toJson((Object[]) obj);
				sb.append("\"").append(s1).append("\":").append(s3).append(",");

			} else if (obj instanceof Map) {
				Map map1 = removeListAttr((Map) obj);
				sb.append("\"").append(s1).append("\":").append(toJson(map1))
						.append(",");
			} else if (!obj.getClass().getName().startsWith("java")) {
				Map map2 = getAttributes(obj);
				map2 = removeListAttr(map2);
				sb.append("\"").append(s1).append("\":").append(toJson(map2))
						.append(",");
			} else {
				sb.append("\"").append(s1).append("\":").append("\"").append(
						obj.toString()).append("\",");
			}
		}

		if (sb.length() == 0) {
			return "{}";
		} else {
			sb.insert(0, '{');
			sb.deleteCharAt(sb.length() - 1);
			sb.append('}');
			return sb.toString();
		}
	}

    public static String toJson(Object aobj[])
    {
        if(aobj.length == 0)
        {
            return "[]";
        }
        String s = "";
        Object aobj1[] = aobj;
        int i = aobj1.length;
        for(int j = 0; j < i; j++)
        {
            Object obj = aobj1[j];
            if(obj instanceof Map)
            {
                Map map = (Map)obj;
                map = removeListAttr(map);
                s = (new StringBuilder()).append(s).append(toJson(map)).append(",").toString();
                continue;
            }
            if(obj instanceof String)
            {
                s = (new StringBuilder()).append(s).append((String)obj).append(",").toString();
            } else
            {
                Map map1 = getAttributes(obj);
                map1 = removeListAttr(map1);
                s = (new StringBuilder()).append(s).append(toJson(map1)).append(",").toString();
            }
        }

        return (new StringBuilder()).append("[").append(s.substring(0, s.length() - 1)).append("]").toString();
    }

    public static String toJson(List list)
    {
        return toJson(list.toArray());
    }

    public static Map getAttributes(Object obj)
    {
        Class class1 = obj.getClass();
        try
        {
            Method method = class1.getMethod("isProxy", new Class[0]);
            Boolean boolean1 = (Boolean)method.invoke(obj, new Object[0]);
            if(boolean1.booleanValue())
            {
                class1 = class1.getSuperclass();
            }
        }
        catch(Exception exception) { }
        HashMap hashmap = new HashMap();
        Object aobj[] = class1.getFields();
        int i = aobj.length;
        for(int j = 0; j < i; j++)
        {
            Field field = (Field) aobj[j];
            try
            {
                Object obj1 = field.get(obj);
                hashmap.put(field.getName(), obj1);
            }
            catch(Exception exception1) { }
        }

        aobj = class1.getDeclaredMethods();
        i = aobj.length;
        for(int k = 0; k < i; k++)
        {
            Method method1 = (Method) aobj[k];
            String s = method1.getName();
            if(s.equals("getClass"))
            {
                continue;
            }
            if(s.startsWith("get"))
            {
                String s1 = s.substring(3);
                if(s1.length() == 1)
                {
                    s1 = s1.toLowerCase();
                } else
                {
                    s1 = (new StringBuilder()).append(s1.substring(0, 1).toLowerCase()).append(s1.substring(1)).toString();
                }
                try
                {
                    Object obj2 = method1.invoke(obj, new Object[0]);
                    hashmap.put(s1, obj2);
                }
                catch(Exception exception2) { }
                continue;
            }
            if(!s.startsWith("is"))
            {
                continue;
            }
            String s2 = s.substring(2);
            if(s2.length() == 1)
            {
                s2 = s2.toLowerCase();
            } else
            {
                s2 = (new StringBuilder()).append(s2.substring(0, 1).toLowerCase()).append(s2.substring(1)).toString();
            }
            try
            {
                Object obj3 = method1.invoke(obj, new Object[0]);
                hashmap.put(s2, obj3);
            }
            catch(Exception exception3) { }
        }

        return hashmap;
    }

    private static Map removeListAttr(Map map)
    {
        HashMap hashmap = new HashMap();
        Iterator iterator = map.entrySet().iterator();
        do
        {
            if(!iterator.hasNext())
            {
                break;
            }
            Object obj = iterator.next();
            java.util.Map.Entry entry = (java.util.Map.Entry)obj;
            if(!(entry.getValue() instanceof List))
            {
                hashmap.put((String)entry.getKey(), entry.getValue());
            }
        } while(true);
        return hashmap;
    }
}
